using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
using Geometry;
using Photogrammetry.Transformation;

namespace Raster.ReSampling
{
    public class ReSample<T> where T:Photogrammetry.Transformation.ITransform
    {
        PointCloud grid;
        /// <summary>
        /// Constructor method
        /// This constructor assumes that the Transform Object is initialized before and the transformation parameters are calculated.
        /// </summary>
        /// <param name="transformObject">The transformation object to be used.</param>
        /// <param name="vertices">Vertices to be transformed as a colored point array.</param>
        public ReSample(T transformObject,ColoredPoint[] vertices)
        {

            double minX;
            double minY;
            double maxX;
            double maxY;
            ColoredPoint[] transformedVertices = transformObject.Transform(vertices,out minX,out minY,out maxX,out maxY);
            grid = new PointCloud((int)(1+maxY - minY), (int)(1+maxX - minX));
            foreach (ColoredPoint vertice in transformedVertices)
            {
                vertice.X = vertice.X - minX;
                vertice.Y = vertice.Y - minY;
                grid.Add(vertice);
            }
            Bitmap reSampled = new Bitmap((int)(1 + maxX - minX), (int)(1 + maxY - minY), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            for (int i = 0; i < (int)(1 + maxY - minY); i++)
            {
                for (int j = 0; j < (int)(1 + maxX - minX); j++)
                {
                    reSampled.SetPixel(j,i,grid.FindNear(j, i, 4));
                }
                Console.WriteLine(i);
            }
            reSampled.Save("f:\\reSampled.bmp", System.Drawing.Imaging.ImageFormat.Tiff);
            Console.WriteLine("fin");
            Console.ReadKey();
        }
    }
}
